{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.12"
    },
    "colab": {
      "name": "det.ipynb",
      "provenance": []
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "gc9AQ3iZWxxX"
      },
      "source": [
        "import numpy as np\n",
        "import time\n",
        "\n",
        "#Минор\n",
        "def submatrix(M, c):\n",
        "    B = [[1] * len(M) for i in range(len(M))]\n",
        "\n",
        "    for l in range(len(M)):\n",
        "        for k in range(len(M)):\n",
        "            B[l][k] = M[l][k]\n",
        "\n",
        "    B.pop(0)\n",
        "\n",
        "    for i in range(len(B)):\n",
        "        B[i].pop(c)\n",
        "    return B\n",
        "\n",
        "#Определитель методом Лапласа\n",
        "def det_L(M):\n",
        "    X = 0\n",
        "    if len(M) != len(M[0]):\n",
        "        print('Матрица не квадратная')\n",
        "    else:\n",
        "        if len(M) <= 2:\n",
        "            return M[0][0] * M[1][1] - M[0][1] * M[1][0]\n",
        "        else:\n",
        "            for i in range(len(M)):\n",
        "                X = X + ((-1) ** (i)) * M[0][i] * det_L(submatrix(M, i))\n",
        "    return X"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZS_0KnL2Wxxc",
        "outputId": "d94032c0-19d6-4560-f307-7e240aa73383"
      },
      "source": [
        "a = np.random.rand(9, 9)\n",
        "print (a)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[0.05248664 0.62989175 0.07403709 0.65745131 0.20575552 0.50526009\n",
            "  0.63078817 0.24260964 0.63243584]\n",
            " [0.05205854 0.64450075 0.10677567 0.2505642  0.971649   0.2547137\n",
            "  0.34641867 0.98833176 0.58685165]\n",
            " [0.67821543 0.98150951 0.6839764  0.94867451 0.49665479 0.02537096\n",
            "  0.81264536 0.29308287 0.95758773]\n",
            " [0.25799688 0.89067484 0.9223908  0.00465071 0.09641057 0.78296139\n",
            "  0.3271132  0.62075676 0.57194499]\n",
            " [0.16291325 0.46961288 0.99808642 0.77674709 0.10530458 0.9843679\n",
            "  0.28204693 0.34524931 0.815628  ]\n",
            " [0.9691605  0.17674436 0.89686467 0.31225912 0.6011855  0.96877932\n",
            "  0.4476104  0.93433377 0.56883673]\n",
            " [0.82101394 0.5615612  0.72112595 0.93000743 0.33537901 0.19651114\n",
            "  0.6112784  0.61147906 0.975751  ]\n",
            " [0.81136166 0.05365163 0.83429683 0.70201509 0.10199339 0.09276835\n",
            "  0.0038309  0.35416053 0.38151839]\n",
            " [0.48451748 0.1967548  0.00245473 0.2482714  0.79405789 0.99634107\n",
            "  0.68066725 0.85702084 0.52818383]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rGos9-UEWxxe",
        "outputId": "3edb5306-beb6-4421-99cb-4315d49864b2"
      },
      "source": [
        "start_time = time.time()\n",
        "print (np.linalg.det(a))\n",
        "\n",
        "print(\"--- %s seconds ---\" % (time.time() - start_time))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.0063744921509091895\n",
            "--- 0.0029976367950439453 seconds ---\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sM0aNa9AWxxf",
        "outputId": "b3944c05-35ca-44c9-9c61-d861f1aadc38"
      },
      "source": [
        "start_time2 = time.time()\n",
        "print (det_L(a))\n",
        "print(\"--- %s seconds ---\" % (time.time() - start_time2))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.006374492150909211\n",
            "--- 6.488999128341675 seconds ---\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WMdjyNTTWxxf"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pLwRm6ICWxxf"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J837V331Wxxg"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}